//
//  AUEssenceTopicCommentView.swift
//  Alamofire
//
//  Created by Miss_Special on 2018/4/27.
//  Comment

import UIKit

let AUEssenceTopicCommentCell_identifier = "AUEssenceTopicCommentCell_identifier"

class AUEssenceTopicCommentView: UIView {
    
    var comments: [AUEssenceTopicCommentModel] = [] {
        didSet {
            tableView.reloadData()
        }
    }
    
    fileprivate lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        let bundle = Bundle(for: type(of: self))
        tableView.register(
            UINib(nibName: "AUEssenceTopicCommentCell", bundle: bundle),
            forCellReuseIdentifier: AUEssenceTopicCommentCell_identifier
        )
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100
        tableView.dataSource = self
        return tableView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension AUEssenceTopicCommentView {
    func setupUI() {
        addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

extension AUEssenceTopicCommentView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return comments.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: AUEssenceTopicCommentCell_identifier, for: indexPath) as! AUEssenceTopicCommentCell
        cell.commentModel = comments[indexPath.row]
        return cell
    }
}
